package com.rayc.work;

import com.rabbitmq.client.*;
import com.rayc.config.RabbitmMQClient;
import org.junit.Test;

import java.io.IOException;

public class Consumer2 {
   @Test
    public void consumer() throws Exception {
        //1.获取Connection
        Connection connection = RabbitmMQClient.getConnection();
        //2.创建Channel
       final Channel channel = connection.createChannel();
        //3.声明队列
        channel.queueDeclare("work",true,false,false,null);
       //指定当前消费者一次消费多少
       channel.basicQos(1);
       //4.开始监听队列Queue
       DefaultConsumer consumer = new DefaultConsumer(channel){
           @Override
           public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
               try {
                   Thread.sleep(200);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("消费者2接收到消息："+ new String(body,"utf-8"));
               channel.basicAck(envelope.getDeliveryTag(), false);
           }
       };
       channel.basicConsume("work", false,consumer);
        System.out.println("消费者2开始监听队列");
        System.in.read();
        channel.close();
       connection.close();
    }
}
